<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="任务调度" tabindex="-1"><a class="header-anchor" href="#任务调度"><span>任务调度</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#defining-schedules">定义调度</a>
<ul>
<li><a href="#scheduling-artisan-commands">Artisan 命令调度</a></li>
<li><a href="#scheduling-queued-jobs">队列任务调度</a></li>
<li><a href="#scheduling-shell-commands">Shell 命令调度</a></li>
<li><a href="#schedule-frequency-options">调度频率设置</a></li>
<li><a href="#timezones">时区</a></li>
<li><a href="#preventing-task-overlaps">避免任务重复</a></li>
<li><a href="#running-tasks-on-one-server">任务只运行在一台服务器上</a></li>
<li><a href="#background-tasks">后台任务</a></li>
<li><a href="#maintenance-mode">维护模式</a></li>
</ul>
</li>
<li><a href="#task-output">任务输出</a></li>
<li><a href="#task-hooks">任务钩子</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>过去，你可能需要在服务器上为每一个调度任务去创建 Cron 入口。但是这种方式很快会变得不友好，因为这些任务调度不在源代码中，并且你每次都需要通过 SSH 链接登录到服务器中才能增加 Cron 入口。</p>
<p>Laravel 命令行调度器允许你在 Laravel 中对命令调度进行清晰流畅的定义。且使用这个任务调度器时，你只需要在你的服务器上创建单个 Cron 入口。你的任务调度在 <code v-pre>app/Console/Kernel.php</code> 的 <code v-pre>schedule</code> 方法中进行定义。为了帮助你更好的入门，这个方法中有个简单的例子。</p>
<h3 id="启动调度器" tabindex="-1"><a class="header-anchor" href="#启动调度器"><span>启动调度器</span></a></h3>
<p>当使用这个调度器时，你只需要把下面的 Cron 入口添加到你的服务器中。如果你不知道怎么在服务器中添加 Cron 入口，可以考虑使用一些服务来管理 Cron 入口，比如 <a href="https://forge.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Forge</a> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">*</span> <span class="token operator">*</span> <span class="token operator">*</span> <span class="token operator">*</span> <span class="token operator">*</span> cd <span class="token operator">/</span>path<span class="token operator">-</span>to<span class="token operator">-</span>your<span class="token operator">-</span>project <span class="token operator">&amp;&amp;</span> php artisan schedule<span class="token punctuation">:</span>run <span class="token operator">>></span> <span class="token operator">/</span>dev<span class="token operator">/</span><span class="token constant">null</span> <span class="token number">2</span><span class="token operator">></span><span class="token operator">&amp;</span><span class="token number">1</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个 Cron 会每分钟执行一次 Laravel 的命令行调度器。当 <code v-pre>schedule:run</code> 命令被执行的时候，Laravel 会根据你的调度执行预定的程序。</p>
<h2 id="定义调度" tabindex="-1"><a class="header-anchor" href="#定义调度"><span>定义调度</span></a></h2>
<p>你可以在 <code v-pre>App\Console\Kernel</code> 类的 <code v-pre>schedule</code> 方法中定义所有的调度任务。 在开始之前，让我们来看一个例子。在此例中，我们计划每天午夜调用一个闭包。在闭包中，我们会执行一个数据库查询来清空一张表：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Console</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>DB</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Scheduling<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Kernel</span> <span class="token keyword">as</span> ConsoleKernel<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Kernel</span> <span class="token keyword">extends</span> <span class="token class-name">ConsoleKernel</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 应用中自定义的 Artisan 命令</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$commands</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">//</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 定义计划任务</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Scheduling<span class="token punctuation">\</span>Schedule</span>  <span class="token parameter">$schedule</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">schedule</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Schedule</span> <span class="token variable">$schedule</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'recent_users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>除了使用闭包来定义任务调度外，你也可以使用 <a href="https://secure.php.net/manual/en/language.oop5.magic.php#object.invoke" target="_blank" rel="noopener noreferrer">invokable objects</a>。<br>
Invokable objects 指的是 PHP 中含有 <code v-pre>__invoke</code> 方法的类。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DeleteRecentUsers</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="artisan-命令调度" tabindex="-1"><a class="header-anchor" href="#artisan-命令调度"><span>Artisan 命令调度</span></a></h3>
<p>除了调用闭包这种方式来调度外，你还可以调用 <a href="https://learnku.com/docs/laravel/6.x/artisan" target="_blank" rel="noopener noreferrer">Artisan commands</a> 和操作系统命令。例如，你可以给 <code v-pre>command</code> 方法传递命令名称或者类来调度一个 Artisan 命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send Taylor --force'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token class-name static-context">EmailsCommand</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'--force'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="队列任务调度" tabindex="-1"><a class="header-anchor" href="#队列任务调度"><span>队列任务调度</span></a></h3>
<p><code v-pre>job</code> 方法可以用来调度 <a href="https://learnku.com/docs/laravel/6.x/queues" target="_blank" rel="noopener noreferrer">队列任务</a> 。 此方法提供了一种快捷方式来调度任务，而无需使用 <code v-pre>call</code> 方法创建闭包来调度任务：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">job</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Heartbeat</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">everyFiveMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 分发任务到「heartbeats」队列...</span></span>
<span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">job</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Heartbeat</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'heartbeats'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">everyFiveMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="shell-调度命令" tabindex="-1"><a class="header-anchor" href="#shell-调度命令"><span>Shell 调度命令</span></a></h3>
<p><code v-pre>exec</code> 方法可用于向操作系统发送命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'node /home/forge/script.js'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="调度频率设置" tabindex="-1"><a class="header-anchor" href="#调度频率设置"><span>调度频率设置</span></a></h3>
<p>你还可以给你的任务分配多种调度方式</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>-&gt;cron('* * * * *');</code></td>
<td>自定义 Cron 计划执行任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyMinute();</code></td>
<td>每分钟执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyFiveMinutes();</code></td>
<td>每五分钟执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyTenMinutes();</code></td>
<td>每十分钟执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyFifteenMinutes();</code></td>
<td>每十五分钟执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyThirtyMinutes();</code></td>
<td>每三十分钟执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;hourly();</code></td>
<td>每小时执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;hourlyAt(17);</code></td>
<td>每小时第17分钟执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;daily();</code></td>
<td>每天午夜执行一次任务（译者注：每天零点）</td>
</tr>
<tr>
<td><code v-pre>-&gt;dailyAt('13:00');</code></td>
<td>每天 13 点执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;twiceDaily(1, 13);</code></td>
<td>每天 1 点及 13 点各执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;weekly();</code></td>
<td>每周执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;weeklyOn(1, '8:00');</code></td>
<td>每周一的 8 点执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;monthly();</code></td>
<td>每月执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;monthlyOn(4, '15:00');</code></td>
<td>每月 4 号的 15 点 执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;quarterly();</code></td>
<td>每季度执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;yearly();</code></td>
<td>每年执行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;timezone('America/New_York');</code></td>
<td>设置时区</td>
</tr>
</tbody>
</table>
<p>结合额外的限制条件，我们可以生成在一周中的特定时间运行的计划任务。例如，在每周一执行命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 每周一 13:00 执行...</span></span>
<span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">weekly</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">mondays</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">at</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'13:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 工作日（周一至周五） 8 点 至 17 点每小时执行一次...</span></span>
<span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">weekdays</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">hourly</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">timezone</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'America/Chicago'</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">between</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'8:00'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'17:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>额外的限制条件列表如下：</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>-&gt;weekdays();</code></td>
<td>限制任务在工作日执行</td>
</tr>
<tr>
<td><code v-pre>-&gt;weekends();</code></td>
<td>限制任务在周末执行</td>
</tr>
<tr>
<td><code v-pre>-&gt;sundays();</code></td>
<td>限制任务在周日执行</td>
</tr>
<tr>
<td><code v-pre>-&gt;mondays();</code></td>
<td>限制任务在周一执行</td>
</tr>
<tr>
<td><code v-pre>-&gt;tuesdays();</code></td>
<td>限制任务在周二执行</td>
</tr>
<tr>
<td><code v-pre>-&gt;wednesdays();</code></td>
<td>限制任务在周三执行</td>
</tr>
<tr>
<td><code v-pre>-&gt;thursdays();</code></td>
<td>限制任务在周四执行</td>
</tr>
<tr>
<td><code v-pre>-&gt;fridays();</code></td>
<td>限制任务在周五执行</td>
</tr>
<tr>
<td><code v-pre>-&gt;saturdays();</code></td>
<td>限制任务在周六执行</td>
</tr>
<tr>
<td><code v-pre>-&gt;between($start, $end);</code></td>
<td>限制任务在 <code v-pre>$start</code> 和 <code v-pre>$end</code> 区间执行</td>
</tr>
<tr>
<td><code v-pre>-&gt;when(Closure);</code></td>
<td>限制任务在闭包返回为真时执行</td>
</tr>
<tr>
<td><code v-pre>-&gt;environments($env);</code></td>
<td>限制任务在特定环境中执行</td>
</tr>
</tbody>
</table>
<h4 id="时间范围限制" tabindex="-1"><a class="header-anchor" href="#时间范围限制"><span>时间范围限制</span></a></h4>
<p><code v-pre>between</code> 方法可用于限制任务在一天中的某个时间段执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'reminders:send'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">hourly</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">between</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'7:00'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'22:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>相似地， <code v-pre>unlessBetween</code> 方法也可用于限制任务不在一天中的某个时间段执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'reminders:send'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">hourly</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">unlessBetween</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'23:00'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'4:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="闭包检测限制" tabindex="-1"><a class="header-anchor" href="#闭包检测限制"><span>闭包检测限制</span></a></h4>
<p><code v-pre>when</code> 方法可根据检测结果来执行任务。换言之，若给定的闭包返回 <code v-pre>true</code> ，若无其他限制条件阻止，任务就会一直执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">when</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>skip</code> 可看作是 <code v-pre>when</code> 的逆方法。若 <code v-pre>skip</code> 方法返回 <code v-pre>true</code> ，任务将不会执行</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">skip</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当链式调用 <code v-pre>when</code> 方法时，仅当所有 <code v-pre>when</code> 都返回 <code v-pre>true</code> 时，任务才会执行。</p>
<h4 id="环境限制" tabindex="-1"><a class="header-anchor" href="#环境限制"><span>环境限制</span></a></h4>
<p><code v-pre>environments</code> 方法可限制任务在给定环境中执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">environments</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'staging'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'production'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="时区" tabindex="-1"><a class="header-anchor" href="#时区"><span>时区</span></a></h3>
<p><code v-pre>timezone</code> 方法可指定任务在给定时区中的时间执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'report:generate'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">timezone</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'America/New_York'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">at</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'02:00'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>若想给所有计划任务分配相同的时区，那么需要在 <code v-pre>app/Console/Kernel.php</code> 文件中定义 <code v-pre>scheduleTimezone</code> 方法。这个方法会返回一个默认时区，最终分配给所有计划任务：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取计划事件默认使用的时区。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token punctuation">\</span>DateTimeZone<span class="token punctuation">|</span><span class="token keyword">string</span><span class="token punctuation">|</span><span class="token keyword">null</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">scheduleTimezone</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'America/Chicago'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：请记住，有些时区会使用夏令时。当夏令时发生调整时，你的任务可能会执行两次，甚至根本不会执行。因此，我们建议尽可能避免使用时区来安排计划任务。</p>
</blockquote>
<h3 id="避免任务重复" tabindex="-1"><a class="header-anchor" href="#避免任务重复"><span>避免任务重复</span></a></h3>
<p>默认情况下，即使之前的任务实例还在执行，调度内的任务也会执行。为避免这种情况的发生，你可以使用 <code v-pre>withoutOverlapping</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withoutOverlapping</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在此例中，若 <code v-pre>emails:send</code> <a href="https://learnku.com/docs/laravel/6.x/artisan" target="_blank" rel="noopener noreferrer">Artisan 命令</a> 还未运行，那它将会每分钟执行一次。当你的任务执行时间非常不确定，导致你无法准确预测任务的执行时间，那 <code v-pre>withoutOverlapping</code> 方法会特别有用。</p>
<p>如有需要，你可以在「without overlapping」锁过期之前，指定它的过期时间。默认情况下，这个锁会在 24 小时后过期。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withoutOverlapping</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="任务只运行在一台服务器上" tabindex="-1"><a class="header-anchor" href="#任务只运行在一台服务器上"><span>任务只运行在一台服务器上</span></a></h3>
<blockquote>
<p>注意：你的应用默认缓存驱动必须是 <code v-pre>memcached</code> 或 <code v-pre>redis</code> 才能使用这个特性。除此之外，所有服务器必须使用同一台中央缓存服务器来通信。</p>
</blockquote>
<p>若你的应用在多台服务器中运行，你可能需要限制某个计划任务仅在单台服务器上运行。假如你有一个计划任务：在每周五晚生成一份新报告。若此任务调度器在三台服务器上运行，那么该任务就会在三台服务器上运行并生成三份报告。这样不好。</p>
<p>当你定义计划任务时，可使用 <code v-pre>onOneServer</code> 方法表明该任务仅在单台服务器上运行。如此，第一台获取到该任务的服务器，会给它附加一个原子锁，这样便可防止其他服务器在同一时间内执行同一个任务：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'report:generate'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">fridays</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">at</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'17:00'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onOneServer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="后台任务" tabindex="-1"><a class="header-anchor" href="#后台任务"><span>后台任务</span></a></h3>
<p>默认情况下，计划同时运行的多个命令将会顺序执行。若你有长时间运行的命令，这可能导致后续命令的启动时间比预期的更晚。你可以使用 <code v-pre>runInBackground</code> 方法让命令在后台运行，如此，它们便可同时运行了:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'analytics:report'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">runInBackground</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：仅能在通过 <code v-pre>command</code> 和 <code v-pre>exec</code> 方法调度任务时，方能使用 <code v-pre>runInBackground</code> 方法。</p>
</blockquote>
<h3 id="维护模式" tabindex="-1"><a class="header-anchor" href="#维护模式"><span>维护模式</span></a></h3>
<p>Laravel 的队列任务在 <a href="https://learnku.com/docs/laravel/5.8/configuration#maintenance-mode" target="_blank" rel="noopener noreferrer">维护模式</a> 下不会运行。因为我们不想你的调度任务干扰到你服务器上可能还未完成的项目。不过，如果你确实是想在维护模式下强制调度任务执行，你可以使用 <code v-pre>evenInMaintenanceMode</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">evenInMaintenanceMode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="任务输出" tabindex="-1"><a class="header-anchor" href="#任务输出"><span>任务输出</span></a></h2>
<p>Laravel 调度器提供了一些方便的方法来处理调度任务输出。首先，你可以使用 <code v-pre>sendOutputTo</code> 方法来输出到文件以便于后续检查：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">sendOutputTo</span><span class="token punctuation">(</span><span class="token variable">$filePath</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果希望将输出 <code v-pre>附加</code> 到给定文件，可以使用 <code v-pre>appendOutputTo</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">appendOutputTo</span><span class="token punctuation">(</span><span class="token variable">$filePath</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>使用 <code v-pre>emailOutputTo</code> 方法，你可以将输出发送到指定邮箱。在使用邮件发送之前，你需要配置 Laravel 的 <a href="https://learnku.com/docs/laravel/6.0/mail" target="_blank" rel="noopener noreferrer">邮件服务</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">sendOutputTo</span><span class="token punctuation">(</span><span class="token variable">$filePath</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">emailOutputTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你只想在命令执行失败的时候输出到邮箱，你可以使用 <code v-pre>emailOutputOnFailure</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">emailOutputOnFailure</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：<code v-pre>emailOutputTo</code>，<code v-pre>emailOutputOnFailure</code>，<code v-pre>sendOutputTo</code> 和 <code v-pre>appendOutputTo</code> 方法是 command 和 exec 独有的。</p>
</blockquote>
<h2 id="任务钩子" tabindex="-1"><a class="header-anchor" href="#任务钩子"><span>任务钩子</span></a></h2>
<p>使用 <code v-pre>before</code> 和 <code v-pre>after</code> 方法，你可以在调度任务执行前或者执行后来执行特定代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">before</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">             <span class="token comment">// 任务即将开始...</span></span>
<span class="line">         <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">             <span class="token comment">// 任务完成...</span></span>
<span class="line">         <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>onSuccess</code>和<code v-pre>onFailure</code>方法允许您指定计划任务成功或失败时要执行的代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">onSuccess</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">             <span class="token comment">// 任务成功...</span></span>
<span class="line">         <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">onFailure</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">             <span class="token comment">// 任务失败...</span></span>
<span class="line">         <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="pinging-网址" tabindex="-1"><a class="header-anchor" href="#pinging-网址"><span>Pinging 网址</span></a></h4>
<p>使用 <code v-pre>pingBefore</code> 和 <code v-pre>thenPing</code> 方法，你可以在任务执行前或者执行后来 ping 指定的 URL。这个方法在通知外部服务（如 <a href="https://envoyer.io/" target="_blank" rel="noopener noreferrer">Laravel Envoyer</a>）计划任务将要执行或者已完成时将会特别有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">pingBefore</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">thenPing</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>只有在给定条件为 <code v-pre>true</code> 时，才能使用 <code v-pre>pingBeforeIf</code> 和 <code v-pre>thenPingIf</code> 方法 ping 指定的 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">pingBeforeIf</span><span class="token punctuation">(</span><span class="token variable">$condition</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">thenPingIf</span><span class="token punctuation">(</span><span class="token variable">$condition</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在任务成功或失败时，使用<code v-pre>pingOnSuccess</code>和<code v-pre>pingOnFailure</code>方法 ping 给定网址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">pingOnSuccess</span><span class="token punctuation">(</span><span class="token variable">$successUrl</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">pingOnFailure</span><span class="token punctuation">(</span><span class="token variable">$failureUrl</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>所有 ping 方法都需要 Guzzle HTTP 库。你可以使用 Composer 来添加 Guzzle 到你的项目中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> guzzlehttp<span class="token operator">/</span>guzzle</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/scheduling/5169" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/sch...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/scheduling/5169" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/sch...</a></p>
</blockquote>
</div></template>


